

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.Equipment;
import com.loong.loong.hik.service.EquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 设备管理
 *
 * @author jinyiang
 * @date 2023-03-28 11:13:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/equipment" )
@Api(value = "equipment", tags = "设备管理管理")
public class EquipmentController {

    private final  EquipmentService equipmentService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param equipment 设备管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_equipment_view')" )
    public R getEquipmentPage(Page page, Equipment equipment) {
		return R.ok(equipmentService.page(page,
				Wrappers.query(equipment).lambda()
						.like(StringUtils.isNotEmpty(equipment.getVideoName()), Equipment::getVideoName,equipment.getVideoName())
						.between(StringUtils.isNotEmpty(equipment.getStartTime())&&StringUtils.isNotEmpty(equipment.getEndTime()),Equipment::getCreateTime,equipment.getStartTime(),equipment.getEndTime())));
    }


    /**
     * 通过id查询设备管理
     * @param videoId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{videoId}" )
    @PreAuthorize("@pms.hasPermission('hik_equipment_view')" )
    public R getById(@PathVariable("videoId" ) Integer videoId) {
        return R.ok(equipmentService.getById(videoId));
    }

    /**
     * 新增设备管理
     * @param equipment 设备管理
     * @return R
     */
    @ApiOperation(value = "新增设备管理", notes = "新增设备管理")
    @SysLog("新增设备管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_equipment_add')" )
    public R save(@RequestBody Equipment equipment) {
        return R.ok(equipmentService.save(equipment));
    }

    /**
     * 修改设备管理
     * @param equipment 设备管理
     * @return R
     */
    @ApiOperation(value = "修改设备管理", notes = "修改设备管理")
    @SysLog("修改设备管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_equipment_edit')" )
    public R updateById(@RequestBody Equipment equipment) {
        return R.ok(equipmentService.updateById(equipment));
    }

    /**
     * 通过id删除设备管理
     * @param videoId id
     * @return R
     */
    @ApiOperation(value = "通过id删除设备管理", notes = "通过id删除设备管理")
    @SysLog("通过id删除设备管理" )
    @DeleteMapping("/{videoId}" )
    @PreAuthorize("@pms.hasPermission('hik_equipment_del')" )
    public R removeById(@PathVariable Integer videoId) {
        return R.ok(equipmentService.removeById(videoId));
    }


    /**
     * 导出excel 表格
     * @param equipment 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_equipment_export')" )
    public List<Equipment> export(Equipment equipment) {
        return equipmentService.list(Wrappers.query(equipment));
    }
}
